package com.dragdemo.zhangnn.animotationdemo;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity
        extends
        AppCompatActivity
        implements
        View.OnClickListener
{
	
	private Context		mContext;
	private ListView	listView;
	private TextView	tvStart;
	private TextView	tvEnd;
	private TestAdapter	adapter	  = new TestAdapter();
	private boolean		isRefrush = false;
								  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);
		mContext = this;
		
		FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
		fab.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view) {
				Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
			}
		});
		
		findView();
		
	}
	
	int i = 0;
	
	private void findView() {
		tvStart = (TextView) findViewById(R.id.start);
		tvEnd = (TextView) findViewById(R.id.end);
		listView = (ListView) findViewById(R.id.list_view);
		listView.setAdapter(adapter);
		
		tvStart.setOnClickListener(this);
		tvEnd.setOnClickListener(this);
		testData();
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(mContext,"您点击了第"+position+"item",Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		if (v == tvStart)
		{
			startUpdateUI();
		}
		else if (v == tvEnd)
		{
			endUpdateUI();
		}
	}
	
	private void endUpdateUI() {
		isRefrush=false;
		myHandler.sendEmptyMessageDelayed(2, 200);
	}
	
	private void startUpdateUI() {
		isRefrush=true;
		myHandler.sendEmptyMessageDelayed(1, 100);
	}
	
	private Handler myHandler = new Handler()
	{
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 1:
//					isRefrush=true;
//					break;
				case 2:
					updateUi();
//					isRefrush=false;
					break;
			}
		}
	};
	
	private void updateUi() {
		if (isRefrush)
		{
			if (i < 6)
			{
//				colors[i]=color[i];
				c1.set(i,c2.get(i));
			}
			else
			{
				i = 0;
				c1.clear();
				c1.addAll(c3);
			}
			i++;
			adapter.notifyDataSetChanged();
			startUpdateUI();
		}
		else
		{
			return;
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		
		//noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) { return true; }
		
		return super.onOptionsItemSelected(item);
	}
	
	String[] textContent = {
	                         "第一行",
	                         "第二行",
	                         "第三行",
	                         "第四行",
	                         "第五行",
	                         "第六行"
						   };
						   
	int[]	 color		 = {
	                         R.color.test6,
	                         R.color.test5,
	                         R.color.test4,
	                         R.color.test3,
	                         R.color.test2,
	                         R.color.test1,
						   };

	List c1 = new ArrayList();
	List c2 = new ArrayList();
	List c3 = new ArrayList();
	private void testData(){
		/*****   初始值 **********/
		c1.add(R.color.test6);
		c1.add(R.color.test6);
		c1.add(R.color.test6);
		c1.add(R.color.test6);
		c1.add(R.color.test6);
		c1.add(R.color.test6);

		/********  刷新的值 ********************/
		c2.add(R.color.test6);
		c2.add(R.color.test5);
		c2.add(R.color.test4);
		c2.add(R.color.test3);
		c2.add(R.color.test2);
		c2.add(R.color.test1);

		/**
		 * 初始化
		 */
		c3.add(R.color.test6);
		c3.add(R.color.test6);
		c3.add(R.color.test6);
		c3.add(R.color.test6);
		c3.add(R.color.test6);
		c3.add(R.color.test6);

	}

						   
	int[]	 colors		 = {
		R.color.test6,
		R.color.test6,
		R.color.test6,
		R.color.test6,
		R.color.test6,
		R.color.test6,
};
	int[]	 colors2	 = {
			R.color.test6,
			R.color.test6,
			R.color.test6,
			R.color.test6,
			R.color.test6,
			R.color.test6,
	};
						   
	class TestAdapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			return textContent.length;
		}
		
		@Override
		public Object getItem(int position) {
			return textContent[position];
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MyViewHolder viewHolder;
			if (convertView == null)
			{
				viewHolder = new MyViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
				viewHolder.llaybg = (LinearLayout) convertView.findViewById(R.id.list_item_llay);
				viewHolder.tvName = (TextView) convertView.findViewById(R.id.list_item_name);
				convertView.setTag(viewHolder);
			}
			else
			{
				viewHolder = (MyViewHolder) convertView.getTag();
			}
			viewHolder.tvName.setText(textContent[position].toString());
//			if (position == i)
			                   viewHolder.llaybg.setBackgroundResource((Integer) c1.get(position));
			return convertView;
		}
	}
	
	class MyViewHolder
	{
		TextView	 tvName;
		LinearLayout llaybg;
	}
}
